package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;

import java.util.List;
import java.util.Map;

/**
 * @author wing
 * @version 1.0.0
 */
public class UserAddress extends BaseModel<UserAddress> {
    private static final long serialVersionUID = 1L;

    public static final String userId = "user_id";
    public static final String receipt_name = "receipt_name";
    public static final String receipt_phone = "receipt_phone";
    public static final String province_id = "province_id";
    public static final String city_id = "city_id";
    public static final String district_id = "district_id";
    public static final String address = "address";
    public static final String defaults = "defaults";

    public static final UserAddress dao = new UserAddress();

    @Override
    public Page<UserAddress> paginate(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT ua.*, u.name userName ";
        String exceptSelect = " FROM user_address ua "
        		+ " LEFT JOIN user u ON ua.user_id=u.id ";
        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }
    

    public List<UserAddress> findDefaults(Integer userId){
    	String sql = "SELECT id, receipt_name receiptName, receipt_phone receiptPhone, " +
                    "province_id provinceId, city_id cityId, district_id districtId, " +
                    "address, defaults " +
                " FROM user_address " +
                " WHERE defaults=1 and user_id=? ";

    	return this.find(sql, userId);
    }


    /**
     * 根据user Id查询其所有地址信息
     * @param param
     * @return
     */
    public PageData<UserAddress> paginateByUserId(PageParam<Integer> param){
        String select = "SELECT ua.id, ua.receipt_name receiptName, ua.receipt_phone receiptPhone, " +
                "ua.province_id provinceId, ua.city_id cityId, ua.district_id districtId, " +
                "ua.address, ua.defaults ";

        String exceptSelect = " FROM user_address ua WHERE ua.user_id=? order by ua.defaults DESC,ua.id DESC ";

        Page<UserAddress> page = this.paginate(param.getPageNo(), param.getPageSize(), select, exceptSelect, param.getP());
        return PageUtil.to(page);
    }

}
